package com.muli.muwai.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.muli.muwai.entity.Intfsubjectbook;
import com.muli.muwai.exception.MyDefinedEnum;
import com.muli.muwai.mapper.FinanTableMabber;
import com.muli.muwai.service.FinancetableService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
public class FinancetableServiceImpl extends ServiceImpl<FinanTableMabber, Intfsubjectbook> implements FinancetableService {
    @Autowired
    FinanTableMabber finanTableMabber;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void importdata(List<String[]> financeTable) throws Exception {
        HashSet<String> set = new HashSet<>();
        List<Intfsubjectbook> list = getIntfsubjectbooks(financeTable);
        for (Intfsubjectbook intfsubjectbook : list) {
            String month = intfsubjectbook.getMonth();
            set.add(month);
        }

        if (set.size() == 1) {
            //删除同月份的数据
            String strip = StringUtils.strip(set.toString(), "[]");
            int aa = finanTableMabber.deleteData(strip);
            saveBatch(list);
            System.out.println(aa);
        } else {
            throw new Exception(MyDefinedEnum.ERROR_MONTH.getMsg());
        }
        //存入数据库
        //saveBatch(list);

    }

    private List<Intfsubjectbook> getIntfsubjectbooks(List<String[]> financeTable) throws Exception {
        //将financeTable转换成bean对象
        List<Intfsubjectbook> intfFinanceTables = new ArrayList<>();

        //错误列
        int num = 0;
        for (String[] strings : financeTable) {
            Intfsubjectbook intfFinanceTable = new Intfsubjectbook();
            intfFinanceTable.setFiscalYear(strings[0]);
            intfFinanceTable.setProfit(strings[1]);
            intfFinanceTable.setMonth(strings[2]);
            intfFinanceTable.setSubjectCode(strings[3]);
            intfFinanceTable.setSubjectName(strings[4]);
            intfFinanceTable.setCurrency(strings[5]);
            //intfFinanceTable.setStartDebitBalance(strings[6]);
            try {
                num=7;
                if (strings[6].equals("")) {
                    intfFinanceTable.setStartDebitBalance(Double.valueOf(0));
                } else {
                    intfFinanceTable.setStartDebitBalance(Double.parseDouble(strings[6]));
                }
                num=8;
                //intfFinanceTable.setStartCreditBanlact(strings[7]);
                if (strings[7].equals("")) {
                    intfFinanceTable.setStartCreditBanlact(Double.valueOf(0));
                } else {
                    intfFinanceTable.setStartCreditBanlact(Double.parseDouble(strings[7]));
                }
                num=9;
                //intfFinanceTable.setCurrentDebitAmount(strings[8]);
                if (strings[8].equals("")) {
                    intfFinanceTable.setCurrentDebitAmount(Double.valueOf(0));
                } else {
                    intfFinanceTable.setCurrentDebitAmount(Double.parseDouble(strings[8]));
                }
                num=10;
                //intfFinanceTable.setCurrentCreditAmount(strings[9]);
                if (strings[9].equals("")) {
                    intfFinanceTable.setCurrentCreditAmount(Double.valueOf(0));
                } else {
                    intfFinanceTable.setCurrentCreditAmount(Double.parseDouble(strings[9]));
                }
                num=11;
                //intfFinanceTable.setYearDebitSum(strings[10]);
                if (strings[10].equals("")) {
                    intfFinanceTable.setYearDebitSum(Double.valueOf(0));
                } else {
                    intfFinanceTable.setYearDebitSum(Double.parseDouble(strings[10]));
                }
                num=12;
                //intfFinanceTable.setYearCrebitSum(strings[11]);
                if (strings[11].equals("")) {
                    intfFinanceTable.setYearCrebitSum(Double.valueOf(0));
                } else {
                    intfFinanceTable.setYearCrebitSum(Double.parseDouble(strings[11]));
                }
                num=13;
                //intfFinanceTable.setEndDebitBalance(strings[12]);
                if (strings[12].equals("")) {
                    intfFinanceTable.setEndDebitBalance(Double.valueOf(0));
                } else {
                    intfFinanceTable.setEndDebitBalance(Double.parseDouble(strings[12]));
                }
                num=14;
                //intfFinanceTable.setEndCrebitBalance(strings[13]);
                if (strings[13].equals("")) {
                    intfFinanceTable.setEndCrebitBalance(Double.valueOf(0));
                } else {
                    intfFinanceTable.setEndCrebitBalance(Double.parseDouble(strings[13]));
                }
            } catch (NumberFormatException e) {
                int size = intfFinanceTables.size() + 2;
                throw new Exception("第" + size + "行,第" + num + "列,格式错误");
            }

            intfFinanceTables.add(intfFinanceTable);
        }
        return intfFinanceTables;
    }
}
